package com.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {

    private static final String ORDER_KEY = "order_key";

    /**
     * 生成token
     *
     * @param claims
     * @return
     */
    public static String createToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))
                .sign(Algorithm.HMAC256(ORDER_KEY));
    }

    /**
     * 校验token
     *
     * @param token
     * @return
     */
    public static Map<String, Object> verifyToken(String token) {
        return JWT.require(Algorithm.HMAC256(ORDER_KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }
}
